package eai.tp2.lpco.credentials.auth;

import javax.ejb.Stateful;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import eai.tp2.lpco.database.User;

@Stateful
public class AuthenticationBean implements IAuthentication {

	@PersistenceContext(unitName = "myLpcoCatalogue")
	private EntityManager em;

	private User user = null;

	@Override
	public boolean login(String username, String password) {

		if ((user = em.find(User.class, username)) == null)
			return false;
		else if (user.getUsername().equals(username)
				&& user.getPassword().equals(password))
			return true;
		else
			return false;

	}
	
	@Override
	public boolean isLogged() {
		if(user!=null)
			return true;
		return false;
	}
	
	@Override
	public boolean logout() {
		user = null;
		return true;
	}
}
